#include <scos.h>
#include <stdio.h>
#include "timer.h"

scos_mutex_t mutex;
scos_cond_t condition;
int var=0;

void *taska(void *dummy)
{
  int myvar=1;

  scos_mutex_lock(&mutex);
  while (1)
  {
    while (var == myvar) scos_cond_wait(&condition, &mutex);
    myvar = ++var;
    scos_cond_signal(&condition);
  }
  scos_mutex_unlock(&mutex);
}

void *taskb(void *dummy)
{
  int myvar=0;

  scos_mutex_lock(&mutex);
  while (1)
  {
    while (var == myvar) scos_cond_wait(&condition, &mutex);
    myvar = ++var;
    scos_cond_signal(&condition);
  }
  scos_mutex_unlock(&mutex);
}

void *taskc(void *dummy)
{
  int myvar=1;

  while (1)
  {
    while (var == myvar) sched_yield();
    myvar = ++var;
  }
}

void *taskd(void *dummy)
{
  int myvar=0;

  while (1)
  {
    while (var == myvar) sched_yield();
    myvar = ++var;
  }
}

void MAIN (void)
{
  int i;
  tmr_t timer = tmrInit();
/*
  scos_thread_t thread;

  scos_mutex_init(&mutex);
  scos_cond_init(&condition);

  scos_mutex_lock(&mutex);

  scos_thread_create(&thread, taskc, NULL);
  scos_thread_create(&thread, taskd, NULL);

  tmrStart(&timer);
  scos_mutex_unlock(&mutex);
  usleep(5000000L);
  scos_mutex_lock(&mutex);
  tmrStop(&timer);
*/
  var = 1000000L;

  tmrStart(&timer);
  for (i=0; i < var; i++)
    malloc(10);
  tmrStop(&timer);

  printf("%d switches in %f seconds makes for %f switches/second\n", var, tmrGetValueInUs(&timer)/1000000.0, 
         var/(tmrGetValueInUs(&timer)/1000000.0));
}
